package com.kdyzm.ruoyi.vue.gen.manager;

import com.kdyzm.ruoyi.vue.gen.common.constant.GenConstants;
import com.kdyzm.ruoyi.vue.gen.common.exception.CustomException;
import com.kdyzm.ruoyi.vue.gen.domain.GenTable;
import com.kdyzm.ruoyi.vue.gen.domain.GenTableColumn;
import com.kdyzm.ruoyi.vue.gen.mapper.GenTableColumnMapper;
import com.kdyzm.ruoyi.vue.gen.mapper.GenTableMapper;
import com.kdyzm.ruoyi.vue.gen.properties.GenConfig;
import com.kdyzm.ruoyi.vue.gen.service.IGenTableService;
import com.kdyzm.ruoyi.vue.gen.util.GenUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: Xue Xiaoyong
 * Date: 2023-03-03 11:43
 * Description: <text>
 */
@Component
@Slf4j
public class InitGenTable {
    @Autowired
    private IGenTableService genTableService;

    @Autowired
    private GenConfig genConfig;

    @Autowired
    private GenTableColumnMapper genTableColumnMapper;

    @Autowired
    private GenUtils genUtils;

    String operName = "admin";

    public void generate() {
        GenTable genTable;
        GenTable genSubTable;
        String tableName = genConfig.getTableName();
        genTable = genTableService.selectTableByName(tableName);
        genTable.setTplCategory(GenConstants.TPL_CRUD);
        genUtils.initTable(genTable, operName);
        setGenTableColumn(genTable);
        String subTableName = genConfig.getSubTableName();
        if (!StringUtils.isEmpty(subTableName)){
            genSubTable = genTableService.selectTableByName(subTableName);
            genUtils.initTable(genSubTable, operName);
            genSubTable.setTplCategory(GenConstants.TPL_CRUD);
            setGenTableColumn(genSubTable);

            genTable.setSubTable(genSubTable);
            genTable.setTplCategory(GenConstants.TPL_SUB);
            genTable.setSubTableName(subTableName);
            genTable.setSubTableFkName(genConfig.getSubTableFkName());
        }
        // 生成压缩文件
        byte[] data = genTableService.downloadCode(genTable);
        genCode("ruoyi.zip" , data);
        log.info("生成文件位置:" + System.getProperty("user.dir") + "/ruoyi.zip");

    }
    private void setGenTableColumn(GenTable genTable){
        List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(genTable.getTableName());
        for (GenTableColumn column : genTableColumns)
        {
            GenUtils.initColumnField(column, genTable);
            if(StringUtils.isEmpty(column.getColumnComment()))
            {
                column.setIsEdit(GenConstants.NOT_REQUIRE);
                column.setIsList(GenConstants.NOT_REQUIRE);
                column.setIsQuery(GenConstants.NOT_REQUIRE);
                column.setColumnComment(column.getJavaField());
            }
            if(StringUtils.isEmpty(column.getDictType()))
            {
                column.setDictType("");
            }
        }
        genTable.setColumns(genTableColumns);
    }

    public void genCode(String fileName, byte[] data) {
        try {
            IOUtils.write(data, new FileOutputStream(new File(fileName)));
        } catch (IOException e) {
            log.error("" , e);
        }
    }
}
